package com.jgame.secondarymarket.interfaces.api.product;

import com.jgame.secondarymarket.application.service.product.SpecificationApplicationService;
import com.jgame.secondarymarket.infrastructure.common.constants.RoleConstants;
import com.jgame.secondarymarket.infrastructure.common.model.response.Result;
import com.jgame.secondarymarket.interfaces.assembler.product.SpecificationAssembler;
import com.jgame.secondarymarket.interfaces.dto.product.SpecificationDTO;
import com.jgame.secondarymarket.interfaces.query.page.product.PageSpecificationQuery;

import com.jgame.secondarymarket.interfaces.query.product.UpdateSpecificationQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.validation.Valid;
import java.util.List;

/**
 * @title: SpecificationApi
 * @Author Wangsj
 * @Date: 2021/7/23
 */
@RestController
@RequestMapping("product")
@Api(tags = {"商品规格"})
public class SpecificationApi {

    @Autowired
    SpecificationApplicationService specificationService;

    @ApiOperation(value = "删除商品规格")
    @DeleteMapping("/{productId}/specification")
    public Result del(@PathVariable(value = "productId") Long productId
            , @RequestBody List<Long> specificationId) {
        return Result.op(() -> specificationService.delById(productId, specificationId));
    }

    @ApiOperation(value = "添加商品规格")
    @PostMapping("/{productId}/specification")
    public Result add(@PathVariable(value = "productId") Long productId
            , @Valid @RequestBody List<SpecificationDTO> specifications) {
        return Result.op(() -> specificationService.add(productId, SpecificationAssembler.toSpecificationList(specifications)));
    }

    @ApiOperation(value = "修改商品规格")
    @PutMapping("/{productId}/specification/{specificationId}")
    public Result update(@PathVariable(value = "productId") Long productId
            , @PathVariable(value = "specificationId") Long specificationId
            , @Valid @RequestBody UpdateSpecificationQuery specification) {
        return Result.op(() -> specificationService.update(productId, specificationId, SpecificationAssembler.toSpecification(specification)));
    }

    @ApiOperation(value = "获取商品规格详细信息")
    @GetMapping("/{productId}/specification")
    public Result get(@PathVariable(value = "productId") Long productId
            , @RequestParam(value = "properties") String properties) {
        return Result.ok(specificationService.getOneById(productId, properties));
    }

    @ApiOperation(value = "分页获取商品规格信息")
    @RolesAllowed(RoleConstants.USER)
    @GetMapping("/specification")
    public Result page(@Valid PageSpecificationQuery query) {
        return Result.ok(specificationService.getPage(query));
    }
}
